const AJAX = getApp().ajax,
      $wx = getApp().$wx,
      getColor = getApp().globalData.getColor;

Page({
  data: {
    getColor: getColor(),
    starLevel: {
      taste: 5,
      packaging: 5,
      speed: 5
    },
    textareaNum: 0,
    tempFiles: [],
    commenImgs: []

  },
  
  onLoad(options){
    $wx.setNavColor(getColor().val);  
    this.setData({
      orderId: options.order_id
    });
  },

  onShareAppMessage(){

  },

  //口味
  getTasteStarLevel(e){
    let td = this.data, index = e.currentTarget.dataset.index;
    
    td.starLevel.taste = index + 1;
    this.setData({
      starLevel: td.starLevel
    });
  },
  
  //包装
  getPackagingStarLevel(e){
    let td = this.data, index = e.currentTarget.dataset.index;

    td.starLevel.packaging = index + 1;
    this.setData({
      starLevel: td.starLevel
    });
  },

  //速度
  getSpeedStarLevel(e){
    let td = this.data, index = e.currentTarget.dataset.index;

    td.starLevel.speed = index + 1;
    this.setData({
      starLevel: td.starLevel
    });
  },

  //评价内容
  inputTextarea(e){
    let textareaNum = e.detail.value.length;
    
    this.setData({
      textareaNum
    });
  },

  //上传图片
  uploadImg(){
    let _this = this, td = _this.data;

    if (td.tempFiles.length < 3){
      wx.chooseImage({
        count: 3,
        success(res) {
          td.tempFiles = td.tempFiles.concat(res.tempFilePaths);

          if (td.tempFiles.length > 3){   //防止超图
            td.tempFiles.splice(3,1);
          }

          _this.setData({
            tempFiles: td.tempFiles
          });
        }
      })
    }else{
      G.showToast('上传图片已达到上限啦~');
    }
  },

  /*删除上传图片*/ 
  removeUploadImg(e){
    let td = this.data, item = e.currentTarget.dataset.item;

    td.tempFiles.splice(item,1);

    this.setData({
      tempFiles: td.tempFiles
    });
  },

  // 提交评价
  fomrSubmit(e){
    let _this = this, td = _this.data, 
        form_obj = e.detail.value;

    if(!form_obj.text){
      $wx.showToast('请输入评价内容');
    }else {

      if (td.tempFiles.length > 0){
        
        $wx.uploadFile({
          url: AJAX.uploadImg,
          filePath: td.tempFiles,
          filePathName: 'path',
          name: 'image',
          success(res) {
           let resImg_url = JSON.parse(res.data).data.url;
            td.commenImgs.push(resImg_url);
          },
          complete(){
            _this.commen({
              order_id: td.orderId,
              content: form_obj.text,
              taste_stars: td.starLevel.taste,
              box_stars: td.starLevel.packaging,
              speed_stars: td.starLevel.speed,
              imgs: JSON.stringify(td.commenImgs)
            });
          }
        });
      }else{
        _this.commen({
          order_id: td.orderId,
          content: form_obj.text,
          taste_stars: td.starLevel.taste,
          box_stars: td.starLevel.packaging,
          speed_stars: td.starLevel.speed,
          imgs: ''
        });
      }
    }
  },

  /**
   * 评价
   * @param order_id
   * @param content
   * @param taste_stars
   * @param box_stars
   * @param speed_stars
   * @param imgs
   * */ 
  commen(params = {}){
    wx.showLoading({
      title: '评价中...',
      mask: true
    });

    AJAX.commen(params, (status, data, msg) => {
      wx.hideLoading();
      $wx.showToast(msg);
      
      if(status){
        setTimeout(callBack=>{
          wx.navigateBack();
        }, 1500);
      }
    });
  }

});